<template>
  <div class="app-container">
    <el-row>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm el-form-bottom"
      >
        <el-divider content-position="left"
          ><legend class="divider-line">规格编辑</legend></el-divider
        >
        <el-col :span="8">
          <el-form-item label="规格名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="权重" prop="sort">
            <el-input-number
              v-model="ruleForm.sort"
              :min="0"
              :max="99"
              :step="1"
              step-strictly
            ></el-input-number>
          </el-form-item>
        </el-col>

        <el-divider content-position="left"
          ><legend class="divider-line">规格值，可添加多个</legend></el-divider
        >

        <el-col :span="8">
          <el-form-item>
            <el-button
              type="warning"
              @click="addDomain"
              icon="el-icon-circle-plus-outline"
              >添加</el-button
            >
          </el-form-item>
        </el-col>

        <el-col
          class="add_norm"
          :span="16"
          v-for="(item, index) in domains"
          :key="item.key"
        >
          <el-form-item label="规格值" prop="attrName">
            <el-input v-model="item.name" maxlength="20"></el-input>
          </el-form-item>
          <el-form-item label="权重" prop="attrVal">
            <el-input-number
              v-model="item.sort"
              :min="0"
              :max="99"
              :step="1"
              step-strictly
            ></el-input-number>
          </el-form-item>
          <el-button
            v-show="index != 0"
            @click.prevent="removeDomain(item)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </el-col>
      </el-form>
      <div class="fixed-bottom-20">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即提交</el-button
        >
        <el-button type="success" @click="back">返回</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-row>
  </div>
</template>
<script>
import { editNorm, updateNorm } from "@/api/product";
import { Message } from "element-ui";
export default {
  name: "normAdd",
  data() {
    return {
      ruleForm: {
        name: "",
        sort: ""
      },
      domains: [
        {
          name: "",
          sort: "",
          id: ""
        }
      ],
      rules: {
        name: [
          { required: true, message: "请输入规格名称", trigger: "blur" },
          { max: 20, message: "规格名称不能超过20个字符", trigger: "blur" }
        ],
        sort: [{ required: true, message: "请填写权重", trigger: "blur" }]
      }
    };
  },
  created() {
    this.normId = this.$route.query.id;
    this.getData(this.normId);
  },
  methods: {
    getData(id) {
      editNorm({ id: id }).then(res => {
        this.ruleForm = res.data.data.norm;
        this.domains = res.data.data.attr;
      });
    },
    addDomain() {
      // 新增规格
      this.domains.push({
        name: "",
        sort: "",
        id: "0",
        key: Date.now()
      });
    },
    removeDomain(item) {
      // 移出规格
      var index = this.domains.indexOf(item);
      if (index !== 0) {
        this.domains.splice(index, 1);
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          for (let i in this.domains) {
            if (this.domains[i].name == "") {
              Message.error("规格值不能为空");
              return;
            }
            if (this.domains[i].sort == undefined) {
              Message.error("权重不能为空");
              return;
            }
          }
          let attr = "";
          for (let i in this.domains) {
            let id = this.domains[i].id;
            let name = this.domains[i].name;
            let val = this.domains[i].sort;
            if (name != "" && typeof name != "undefined") {
              attr += id + "," + name + "," + val + ";";
            }
          }
          attr = attr.substring(0, attr.lastIndexOf(";"));
          // this.ruleForm.id = this.normId
          // this.ruleForm.attrVal = this.attrVal
          updateNorm({
            id: this.normId,
            attrVal: attr,
            name: this.ruleForm.name,
            sort: this.ruleForm.sort
          }).then(res => {
            if (!res.data.success) {
              Message.error(res.data.message);
              return;
            }
            Message.success("操作成功");
            history.back();
          });
        }
      });
    },
    resetForm(formName) {
      this.getData(this.normId);
    },
    back() {
      history.back();
    }
  }
};
</script>
<style scoped>
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
/deep/ [class*="el-col-"] {
  float: none;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.add_norm {
  display: flex;
}
.add_norm .el-form-item {
  width: 35%;
}
.add_norm .el-button {
  height: 36px;
  margin-left: 35px;
}

.el-divider {
  margin: 30px 0;
}
.divider-line {
  font-size: 20px;
  font-weight: 300;
}
</style>
